import request from '@/utils/request'
import { defineStore } from 'pinia'
import { ApiRes, BannerItem, GoodsItem, HotGoods } from '@/types/data'
const useHomeStore = defineStore('home', {
  state() {
    return {
      // banner列表 - 初始数据
      bannerList: [] as BannerItem<string>[],
      // 新闻好物列表 - 初始数据
      newGoodsList: [] as GoodsItem[],
      // 人气推荐
      hotGoodsList: [] as HotGoods[]
    }
  },
  actions: {
    // 获取banner列表
    async getBannerList() {
      const res = await request.get<ApiRes<BannerItem<string>[]>>('/home/banner')
      this.bannerList = res.data.result
    },
    // 获取新鲜好物列表
    async getNewList() {
      // setTimeout(async() => {
        const res = await request.get<ApiRes<GoodsItem[]>>('/home/new')
        // console.log(res.data.result);
        this.newGoodsList = res.data.result
      // }, 1000);
    },
    // 获取人气推荐列表
    async getHotList() {
      // setTimeout(async() => {
        const res = await request.get<ApiRes<HotGoods[]>>('/home/hot')
        // console.log(res.data.result);
        this.hotGoodsList = res.data.result
      // }, 1000);
    }
  }
})

export default useHomeStore